//
//  GFLxdDetailViewController.m
//  Qyer
//
//  Created by dlios on 15/10/24.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "GFLxdDetailViewController.h"

@interface GFLxdDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
{
//    UIButton *_dtButton;
    UIButton *_tkButton;
}
@property (nonatomic, strong)GFHeader *header;
@end

@implementation GFLxdDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _rowArray = [NSMutableArray array];
    _sectionArray = [NSMutableArray array];
    _header = [[GFHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * HEIGHT)];
    [self.view addSubview:_header];
    
    _addressLable = [[UILabel alloc] initWithFrame:CGRectMake(5 * WIDTH, 190 * HEIGHT, SCREEN_WIDTH - 10 *WIDTH, 20 * HEIGHT)];
    [_addressLable setTextColor:[NightMode shareNightModeHandle].wordColor];
    [_addressLable setFont:[UIFont systemFontOfSize:WIDTH * 14.]];
    [_header addSubview:_addressLable];
    
    _urlButton = [[UIButton alloc] initWithFrame:CGRectMake(5 * WIDTH, 210 * HEIGHT, SCREEN_WIDTH - 10 * WIDTH, 40 * HEIGHT)];
    [_urlButton.titleLabel setFont:[UIFont systemFontOfSize: WIDTH * 16.]];
    [_urlButton setBackgroundColor:[UIColor lightGrayColor]];
    [_urlButton setTitleColor:[NightMode shareNightModeHandle].wordColor forState:UIControlStateNormal];
    [_urlButton addTarget:self action:@selector(urlButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_header addSubview:_urlButton];
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain]
    ;
    [_table setDelegate:self];
    [_table setDataSource:self];
    [_table setTableHeaderView:_header];
    [self.view addSubview:_table];
    [self getAfn];
}

//- (void)viewWillAppear:(BOOL)animated{
//    _dtButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 65 * WIDTH, 5 * HEIGHT, 30 * WIDTH, 30 * HEIGHT)];
//    [_dtButton setBackgroundImage:[UIImage imageNamed:@"dt.png"] forState:UIControlStateNormal];
//    [_dtButton addTarget:self action:@selector(dtButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationController.navigationBar addSubview:_dtButton];
//    
//    _tkButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 35 * WIDTH, 5 * HEIGHT, 30 * WIDTH, 30 * HEIGHT)];
//    [_tkButton setBackgroundImage:[UIImage imageNamed:@"tk.png"] forState:UIControlStateNormal];
//    [_tkButton addTarget:self action:@selector(tkButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationController.navigationBar addSubview:_tkButton];
//}

//- (void)viewWillDisappear:(BOOL)animated{
//    [_tkButton removeFromSuperview];
////    [_dtButton removeFromSuperview];
//}

#pragma matk-图库
- (void)tkButtonClick: (UIButton *)sender{
    
}

//- (void)dtButtonClick: (UIButton *)sender{
//    GFMapViewController *map = [[GFMapViewController alloc] init];
//    [self.navigationController pushViewController:map animated:YES];
//}

- (void)urlButtonClick{
    WebViewController *web = [[WebViewController alloc] init];
    web.webString = @"http://www.kiyomizudera.or.jp";
    [self.navigationController pushViewController:web animated:YES];
}

- (void)getAfn{
    [AFN getdataWithString:_afnString block:^(id block) {
        NSLog(@"_afnString = %@", _afnString);
        [_header gettitleString:[block objectForKey:@"name_zh_cn"] time:[block objectForKey:@"name_en"] bigImage:[block objectForKey:@"image_url"] userImage:nil];
        [_header.desLable setText:[block objectForKey:@"des"]];
        [_addressLable setText:[block objectForKey:@"address"]];
        [_urlButton setTitle:@"官网: http://www.kiyomizudera.or.jp" forState:UIControlStateNormal];
        
        for (NSDictionary *dic in [block objectForKey:@"attraction_trip_tags"]) {
            GFJnModel *model = [GFJnModel modelWithDictionary:dic];
//            NSLog(@"%@", model.name);
            [_sectionArray addObject:model];
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *tabDic in [dic objectForKey:@"attraction_contents"]) {
                GFJnModel *tbModel = [GFJnModel modelWithDictionary:tabDic];
                tbModel.height = [StringWithHeight HeightWithText:tbModel.des width:SCREEN_WIDTH - 10 * WIDTH font:[UIFont systemFontOfSize:WIDTH * 14.]];
//                NSLog(@"model.height == %f", tbModel.height);
                [arr addObject:tbModel];
            }
            [_rowArray addObject:arr];
        }
        [_table reloadData];
    } BLOCK:^(id BLOCK) {
        
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    GFJnModel *model = [_sectionArray objectAtIndex:section];
    return model.name;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *arr = [_rowArray objectAtIndex:section];
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GFJnModel *model = [[_rowArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    NSLog(@"model.height == %f", model.height);
    return model.height + 140 * HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *guoFeng = @"cell";
    GFLxdDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:guoFeng];
    if (!cell) {
        cell = [[GFLxdDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:guoFeng];
    }
    GFJnModel *model = [[_rowArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell getJnModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LineLayout* lineLayout = [[LineLayout alloc] init];
    ViewController *viewController = [[ViewController alloc] initWithCollectionViewLayout:lineLayout];
    GFJnModel *model = [[_rowArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    viewController.noteArray = model.notes;
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*R
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
